package com.example.monitor.service.impl;

import com.example.monitor.model.DTO.UserAbnormalRecordsQueryDTO;
import com.example.monitor.model.VO.PageResult;
import com.example.monitor.model.VO.ResultVO;
import com.example.monitor.model.VO.UserAbnormalRecordVO;
import com.example.monitor.repository.AlarmRecordRepository;
import com.example.monitor.service.UserAbnormalRecordsService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class UserAbnormalRecordsServiceImpl implements UserAbnormalRecordsService {

    private final AlarmRecordRepository alarmRecordRepository;

    @Override
    public ResultVO<PageResult<UserAbnormalRecordVO>> getUserAbnormalRecords(UserAbnormalRecordsQueryDTO queryDTO) {
        // 构建分页参数
        PageRequest pageRequest = PageRequest.of(
                queryDTO.getPage() - 1,
                queryDTO.getPageSize(),
                Sort.by(Sort.Direction.DESC, "alarmTime"));

        // 查询分页数据
        Page<UserAbnormalRecordVO> page = alarmRecordRepository.findAbnormalRecordsByWorker(
                queryDTO.getWorkerId(),
                pageRequest);

        // 构建返回结果
        PageResult<UserAbnormalRecordVO> result = new PageResult<>();
        result.setTotal(page.getTotalElements());
        result.setRecords(page.getContent());

        return ResultVO.success("查询成功", result);
    }
}
